<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2021/4/28
 * Time: 17:18
 */

namespace App\Services;


use App\Fruit;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Tools\BatchActions;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;

class FruitService
{
    protected $state = ['未发布', '已发布', '已下架'];

    public function gridColumn(\Encore\Admin\Grid $grid, $type = 0): \Encore\Admin\Grid
    {
        $that = $this;
        $grid->column('name', __('项目名称'))->modal('详情', function ($object) use ($that) {
            return $that->detail($object);
        });
        $grid->column('maturity', __('成熟度'));
        $grid->column('industry', __('所属行业'));
        $grid->column('patents', __('申请专利'))->display(function($patents){
            return count($patents);
        });
        $grid->column('user.name', '申请人');
        $grid->column('state', __('发布状态'))->using($this->state)->label(['default','success','default']);
        //$grid->column('review_status', '审核状态')->using(['待审核', '审核通过', '审核未通过'])->label(['warning', 'success', 'default']);
        $grid->column('created_at', __('创建时间'));
        $grid->batchActions(function (BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->where(function ($query) {
                    $query->where('name', 'like', "%{$this->input}%")
                        ->orWhere('industry', 'like', "%{$this->input}%");
                }, '关键字')
                    ->placeholder('项目名称/所属行业');
            });
            $filter->column(1 / 2, function (Filter $filter)  {
                $filter->equal('state', '发布状态')->select($this->state);
            });
        });
        $grid->disableExport();
        return $grid;
    }

    public function detail(Fruit $object)
    {
        $tab = new Tab();
        $tab->add('基本信息', Admin::show($object, function (Show $show) use ($object) {
            $show->panel()->view('show.tab-panel');
            $show->field('name', __('项目名称'));
            $show->field('maturity', __('成熟度'));
            $show->field('industry', __('所属行业'));
            $show->field('desc', __('成果简介'));
            $show->field('patents', __('申请专利'))->as(function($patents){
                return count($patents);
            });
            $show->field('review_status','审核状态')->using(['待审核', '通过', '未通过']);
            if ($object->review_status == 2) {
                $show->field('review_remark','审核备注');
            }
            $show->field('created_at', __('创建时间'));
        }));
        if ($object->is_apply_patent) {
            $tab->add('专利信息', Admin::show($object, function (Show $show) {
                $show->panel()->view('show.tab-panel');
                $show->field('patents', __(' '))->setWidth(12)->unescape()->as(function ($patents) {
                    $arr = collect($patents)->map(function ($item) {
                        return [
                            'patent_sn' => $item['patent_sn'] ?? '',
                            'patent_name' => $item['patent_name'] ?? '',
                            'legal_status' => $item['legal_status'] ?? '',
                        ];
                    })->values()->all();
                    $table = new Table(['专利号', '专利名称', '法律状态'], $arr);
                    return $table;
                });
            }));
        }
        if($object->user){
            $tab->add('申请人信息', Admin::show($object->user, function (Show $show) {
                $show->panel()->view('show.tab-panel');
                $show->field('eno', __('工号'));
                $show->field('department.name', __('部门'));
                $show->field('name', __('姓名'));
                $show->field('mobile', __('电话'));
                $show->field('email', __('邮箱'));
            }));
        }
        return $tab;
    }
}
